<!--特殊区域代理-->
<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12" style="font-size:14px">
        <div class="col-xs-6" id="div_left">
          <div style="margin-top:4px;margin-left:-5px">
            <table class="table table-striped table-bordered table-hover" id="tableListLeft">
              <thead>
                <tr>
                  <td style="height:37px;width:85px">代理类型</td>
                  <td style="height:37px;width:120px">登录账号</td>
                  <td style="height:37px;width:120px">真实姓名</td>
                  <td style="height:37px;width:120px">联系电话</td>
                  <td style="height:37px">代理区域</td>
                  <td style="height:37px;width:140px">上级账号</td>
                  <td style="height:37px;width:102px;text-align:center">选项</td>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td colspan="7" style="height:37px;text-align:center">正在加载……</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
        <div class="col-xs-6" id="div_right">
          <div style="margin-top:4px">
            <table class="table table-striped table-bordered table-hover" id="tableListRight">
              <thead>
              <tr>
                <td style="height:37px">门店名称</td>
                <td style="height:37px;width:120px">联系人</td>
                <td style="height:37px;width:120px">联系电话</td>
                <td style="height:37px">门店地址</td>
                <td style="height:37px;text-align:center;width:50px">选项</td>
              </tr>
              </thead>
              <tbody>
                <tr>
                  <td colspan="5" style="height:37px;text-align:center">选择'区级代理商'查看门店列表</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<div id="div_user_edit" style="display:none;padding:20px 0px;">
  <form class="form-horizontal" id="form_user_edit" role="form">
    <label class="col-sm-3 control-label no-padding-right" for="user_name"><div class="hr-1"></div>登录账号</label>
    <div class="col-sm-7">
      <input type="text" id="user_name" placeholder="登录账号" disabled="disabled" class="form-control"/>
    </div>
    <label class="col-sm-3 control-label no-padding-right" for="user_password"><div class="hr-4"></div>登录密码</label>
    <div class="col-sm-7"><div class="hr-4"></div>
      <input type="password" id="user_password" placeholder="登录密码" class="form-control" />
    </div>
    <label class="col-sm-3 control-label no-padding-right" for="verify_password"><div class="hr-4"></div>确认密码</label>
    <div class="col-sm-7"><div class="hr-4"></div>
      <input type="password" id="verify_password" placeholder="确认密码" class="form-control"/>
    </div>
  </form>
</div>
<script type="text/javascript">
  ;(function($){
    var urlRoute = '/specialArea/';/*请求controller层的url*/
    var listAreaProxy = urlRoute + 'listAreaProxy';/*获取特殊区域代理*/
    var listAreaShop = urlRoute + 'listAreaShop';/*获取区域下的门店列表*/
    var urlEdit = urlRoute + 'editUser';/*修改密码重置密码*/
    thisPage = {
      init : function(){
        this.initDom();
        this.addEvent();
      },
      initDom : function(){
        this.getAreaProxy();
      },
      addEvent: function(){},
      getAreaProxy : function(){
        authHandler('special_area_btn_proxy',function(){
          layerFn.queryGetHintResult(listAreaProxy,null,function(data){
            if(AppKey.code.code200 == data.code){
              var html = thisPage.createLeft(data.data);
              thisPage.tbodyLeft(html);
            }else if(AppKey.code.code198 == data.code){//区代时仅有门店列表
              $('#div_left').remove();//移除左边部分
              $('#div_right').removeAttr('class').addClass('col-xs-12');//撑满
              thisPage.tbodyTextRight('正在加载……');
              thisPage.getAreaShop(data.data);//userId
            }else{
              thisPage.tbodyTextLeft(data.msg);
            }
          });
        },function(){
          thisPage.tbodyTextLeft('没有操作权限');
          layerFn.alert('没有操作权限',AppKey.code.code199);
        });
      },
      tbodyTextLeft : function(msg){
        thisPage.tbodyLeft('<td colspan="7" style="height:37px;text-align:center">'+msg+'</td>');
      },
      tbodyTextRight : function(msg){
        thisPage.tbodyRight('<td colspan="5" style="height:37px;text-align:center">'+msg+'</td>');
      },
      getAreaShop : function(userId){
        authHandler('special_area_btn_shop',function(){
          layerFn.queryGetHintResult(listAreaShop,{userId:userId},function(data){
            if(AppKey.code.code200 == data.code){
              var html = thisPage.createRight(data.data);
              thisPage.tbodyRight(html);
            }else{
              thisPage.tbodyTextRight(data.msg);
            }
          });
        },function(){
          layerFn.alert('没有操作权限',AppKey.code.code199);
        });
      },
      createLeft : function(list){
        function proxyType(type){
          switch (type){
            case 1:
              return '省级分公司';
            case 2:
              return '市级分公司';
            case 3:
              return '区级代理商';
            default:
              return '';
          }
        }
        var html = '';
        for(var a=0;a<list.length;a++){
          var objs = list[a];
          var type3 = (objs.type == 3);
          html += '<tr>';
            html += '<td style="height:37px">'+proxyType(objs.type)+'</td>';
            html += '<td style="height:37px">'+objs.user_name+'</td>';
            html += '<td style="height:37px">'+objs.real_name+'</td>';
            html += '<td style="height:37px">'+objs.phone+'</td>';
            html += '<td style="height:37px" title="'+objs.name+'">'+objs.name+'</td>';
            html += '<td style="height:37px" title="'+(objs.parent != undefined ? objs.parent : '')+'">'+(objs.parent != undefined ? objs.parent : '')+'</td>';
            html += '<td style="height:37px;text-align:center"><a href="javascript:thisPage.editUserDialog(\''+objs.user_id+'\',\''+objs.user_name+'\');" title="修改或重置用户密码" style="outline:none;text-decoration:none;color:#3b8cff">编辑</a>'+(type3 ? '<a href="javascript:thisPage.targetPage(\''+objs.user_id+'\');" title="查看区代采购订单列表" style="outline:none;text-decoration:none;color:#f56c6c;margin-left:4px">订单</a><a href="javascript:thisPage.getAreaShop(\''+objs.user_id+'\');" title="查看门店列表" style="outline:none;text-decoration: none;color:#3b8cff;margin-left:4px">门店</a>' : '')+'</td>';
          html += '</tr>';
        }
        return html;
      },
      createRight : function(list){
        var html = '';
        for(var b=0;b<list.length;b++){
          var objs = list[b];
          html += '<tr>';
            html += '<td style="height:37px;width:240px;max-width:234px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;" title="'+objs.name+'">'+objs.name+'</td>';
            html += '<td style="height:37px">'+objs.contacts+'</td>';
            html += '<td style="height:37px">'+objs.telephone+'</td>';
            html += '<td style="height:37px;width:326px;max-width:320px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;" title="'+objs.address+'">'+objs.address+'</td>';
            html += '<td style="height:37px;text-align:center;width:50px;cursor:pointer;color:#00a0e9" title="查看门店订单列表" onclick="thisPage.targetPage(\''+objs.user_id+'\',\''+objs.kid+'\');">订单</td>';
          html += '</tr>';
        }
        return html;
      },
      tbodyLeft : function(html){
        $('#tableListLeft tbody').html(html);
      },
      tbodyRight : function(html){
        $('#tableListRight tbody').html(html);
      },
      targetPage : function(userId,storeId){
        authHandler('proxy_btn_listData',function(){
          sessionStorage.setItem('userId',userId);
          if(storeId){
            sessionStorage.setItem('storeId',storeId);
          }else{
            sessionStorage.removeItem('storeId');
          }
          window.open('/order.html','_blank').location;
        },function(){
          layerFn.alert('没有操作权限',AppKey.code.code199);
        });
      },
      editUserDialog : function(user_id,user_name){
        authHandler('special_area_row_edit',function(){
          layerFn.addOrEdit('设置密码|重置密码','#div_user_edit',['410px','240px'],function(layerIndex,layero){
            if (verifyFn.inputRequired('#user_password')){
              layerFn.alert('请输入账号密码',AppKey.code.code199);
              return;
            }
            if (verifyFn.inputRequired('#verify_password')){
              layerFn.alert('请输入确认密码',AppKey.code.code199);
              return;
            }
            var password = winFn.getDomValue('#user_password');
            var verify = winFn.getDomValue('#verify_password');
            var bl = verifyFn.checkEqual(password,verify);
            if(bl){
              layerFn.alert('你输入的两次密码不一致',AppKey.code.code199);
              return;
            }
            var params = {
              user_password : winFn.getDomValue('#user_password',true),
              verify_password : winFn.getDomValue('#verify_password',true),
              userId : user_id
            };
            layerFn.submit(urlEdit,params,function(data){
              layerFn.closeIndex(layerIndex);
              layerFn.handleResult(data.msg,data.code);
            });
          });
          winFn.clearFormData('#form_user_edit');
          if(user_id != null){
            $('#user_name').val(user_name);
            $("#user_name").css("cursor","not-allowed");
          }
        },function(){
          layerFn.alert('没有编辑权限',AppKey.code.code199);
        });
      }
    };
    thisPage.init();
    $('script').remove();
  })(jQuery);
</script>